package logic

import (
	"context"
	"fmt"
	"shopping-mall/models"

	"shopping-mall/shopping_rpc/internal/svc"
	"shopping-mall/shopping_rpc/shopping_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RateListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRateListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RateListLogic {
	return &RateListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RateListLogic) RateList(in *shopping_rpc.RateListRequest) (*shopping_rpc.RateListResponse, error) {
	// todo: add your logic here and delete this line
	var rates []struct {
		models.Rate
		UserName string
	}
	if models.DB.Model(&models.Rate{}).Joins("join users on rates.user_id = users.id").Where("rates.product_id = ?", in.ProductId).Select("rates.*,users.name as user_name").Scan(&rates).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	var data []*shopping_rpc.FromRateList
	for _, rate := range rates {
		get := shopping_rpc.FromRateList{
			UserName:  rate.UserName,
			Content:   rate.Content,
			CreatedAt: rate.CreatedAt.String(),
		}
		data = append(data, &get)
	}
	return &shopping_rpc.RateListResponse{
		Rates: data,
	}, nil
}
